package com.example.springboot.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 判断请求来自手机端或者电脑端或者微信端
 *
 * @Author:ChenZhangKun
 * @Date: 2022/3/23 13:58
 */
public class HttpSourceUtil {
    public static String[] deviceArray = new String[]{"android", "iphone", "ios", "windows phone"};

    /**
     * android : 所有android设备 mac os : iphone ipad windows
     * phone:Nokia等windows系统的手机
     *
     * @param request
     * @return
     */
    public static boolean isMobileDevice(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        // 判断是否在里面
        for (String device : deviceArray) {
            if (requestHeader.indexOf(device) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否来自微信端
     *
     * @param request
     * @return
     */
    public static boolean isWeChat(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        if (requestHeader.contains("micromessenger")) {
            return true;
        }
        return false;
    }
}
